﻿<%@ WebHandler Language="C#" Class="DownloadNotics" %>

using System;
using System.Web;
using System.Xml;

public class DownloadNotics : IHttpHandler
{
    string UpFileUrl = Maticsoft.Common.ConfigHelper.GetConfigString("UpFileUrl");
    public void ProcessRequest(HttpContext context)
    {
        GetFiles(context);
    }

    /// <summary>
    /// 获取文件
    /// </summary>
    /// <param name="context"></param>
    public void GetFiles(HttpContext context)
    {
        int _type = int.Parse(context.Request["Type"]);//类型：产品,理发师，发型等
        string _storeId = context.Request["StoreId"] == null ? "0" : context.Request["StoreId"];//门店
        string _fileId = context.Request["FileId"] == null ? "0" : context.Request["FileId"];
        string _fileName = context.Request["fileName"];//文件名
        string filePath = @UpFileUrl+"{0}\\{1}\\"+_fileName;
        BarberShop.Common.Enum.TableType TableType=new BarberShop.Common.Enum.TableType() ;
        switch (_type)
        { 
            case (int)BarberShop.Common.Enum.TableType.BarberInfoTable://理发师
                filePath = string.Format(filePath, "Shop_" + _storeId, "barber");
                TableType=BarberShop.Common.Enum.TableType.BarberInfoTable;
                break;
            case (int)BarberShop.Common.Enum.TableType.HairstyleInfoTable://发型
                filePath = string.Format(filePath, "Shop_" + _storeId, "hair");
                  TableType=BarberShop.Common.Enum.TableType.HairstyleInfoTable;
                break;
            case (int)BarberShop.Common.Enum.TableType.ProductInfoTable://产品
                filePath = string.Format(filePath, "Shop_" + _storeId, "product");
                  TableType=BarberShop.Common.Enum.TableType.ProductInfoTable;
                break;
            case (int)BarberShop.Common.Enum.TableType.StoreInfoTable://门店shop.xml更新
                filePath = string.Format(@UpFileUrl + "{0}\\shop.xml","Shop_"+_storeId) ;
                  TableType=BarberShop.Common.Enum.TableType.StoreInfoTable;
                break;
        }
        down_load_file(context, filePath, TableType,_fileId,_storeId);
       

    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    private void down_load_file(HttpContext context, string filePath, BarberShop.Common.Enum.TableType Type, string _fileId, string _storeId)
    {
        //string filePath = @"E:/下载地址保存2/1.rar";
        System.IO.FileInfo file = new System.IO.FileInfo(filePath);
        if (file.Exists)
        {
            try
            {
                context.Response.Clear();
                context.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                context.Response.AddHeader("Content-Length", file.Length.ToString());
                context.Response.ContentType = "application/octet-stream";
                context.Response.Filter.Close();
                context.Response.WriteFile(file.FullName, false);
                BarberShop.BLL.UpdateFilesBLL.SetFilesStatus(Type,int.Parse(_storeId), int.Parse(_fileId), BarberShop.Common.Enum.FileStatus.Updated);
                context.Response.End();
            }
            catch { }
        }  
    }  
}
